﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class System_AddUser : ATPages
{
    ZSql info = new ZSql();

    ZSql role = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = "";

        if (!IsPostBack)
        {
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            InitInfo();
           // initWare();
            if (Request["uid"] == null)
            {
                if (Request["deptid"] != null)
                {
                    hidDeptid.Value = Request["deptid"].ToString();
                    ddlDept.SelectedValue = hidDeptid.Value;
                }

             //   lblTitle.Text = "添加用户信息";
            }
            else
            {
                InitUser(Request["uid"].ToString());

                if (Request["deptid"] != null)
                {
                    hidDeptid.Value = Request["deptid"].ToString();
                }

               // lblTitle.Text = "用户信息编辑";
            }
            hidModuid.Value = Request["moduid"];
            
        }
    }


    private void InitInfo()
    {
        info.Open("select * from department");
        if (info.m_table.Rows.Count > 0)
        {
            ddlDept.DataSource = info.m_table;
            ddlDept.DataTextField = "deptname";
            ddlDept.DataValueField = "deptid";
            ddlDept.DataBind();
            info.Close();
            ddlDept.Items.Insert(0, new ListItem(">>选择部门", "0"));
            ddlRealname.Items.Insert(0, new ListItem("选择人员", "0"));
        }
        else
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('部门为空请添加部门！')", true);
            ddlDept.Items.Insert(0, new ListItem(">>选择部门", "0"));
            ddlRealname.Items.Insert(0, new ListItem("选择人员", "0"));
        }

        role.Open("select * from role");
        ddlRole.DataSource = role.m_table;
        ddlRole.DataTextField = "role";
        ddlRole.DataValueField = "roleid";
        ddlRole.DataBind();
        role.Close();
    }

    private void InitUser(string userid)
    {
        info.Open("select * from userinfo where userid = " + userid);
        info.NextRow();
        txtUserName.Text = info["username"].ToString();
       // txtRealName.Text = info["realname"].ToString();
        ddlRole.SelectedValue = info["roleid"].ToString();
        ddlDept.SelectedValue = info["deptid"].ToString();
        initName(info["deptid"].ToString());
        ddlRealname.SelectedValue = info["StaffId"].ToString();
        txtbh.Text = info["userbh"].ToString();
        txtIP.Text = info["IP"].ToString();
        info.Close();
    }

    private void initName(string deptid) 
    {
        ZSql zs = new ZSql();
        zs.Open("select staffid,staffname from CS_Staff where deptid=" + deptid);
        ddlRealname.DataSource = zs.m_table;
        ddlRealname.DataTextField = "staffname";
        ddlRealname.DataValueField = "staffid";
       
        ddlRealname.DataBind();
        ddlRealname.Items.Insert(0, new ListItem(">>选择人员", "0"));
        zs.Close();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        //if (ddlRole.SelectedItem.Text!="超级管理员")
        //{
        //    if (ddlWarehous.SelectedIndex==0)
        //    {
        //        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请选择仓库权限！')", true);
               
        //        return;
        //    }
           
        //}
        if (txtUserName.Text == string.Empty)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请输入用户名！')", true);
            return;
        }
        else if (txtPwd.Text == string.Empty && Request["uid"] == null)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请输入密码！')", true);
            return;
        }
        if (ddlRealname.SelectedValue == "0")
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请输入真实姓名！')", true);
            return;
        }

        if (Request["uid"] == null)
        {
            int num = Convert.ToInt16(info.GetScalar("select count(*) from userinfo where username = '" + Common.FormatParameter(txtUserName.Text) + "'"));

            if (num > 0)
            {
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('存在相同的用户名，请修改')", true);
                return;
            }

            info.Open("select * from userinfo where 1=2");
            info.NewRow();

            info["username"] = txtUserName.Text;
            info["password"] = Common.WebEncrypt(txtPwd.Text);
            info["realname"] = ddlRealname.SelectedItem.Text;
            info["StaffId"] = ddlRealname.SelectedValue;
            //info["WarehouseId"] = ddlWarehous.SelectedValue;
            info["deptid"] = ddlDept.SelectedValue;
            info["roleid"] = ddlRole.SelectedValue;
            info["userbh"] = txtbh.Text;
            info["IP"] = txtIP.Text;//ip限制,多个ＩＰ以","号分开
            info.Update();
        }
        else
        {
            //int num = Convert.ToInt16(info.GetScalar("select count(*) from userinfo where username = '" + Common.FormatParameter(txtUserName.Text) + "' and userid <> " + Request["uid"].ToString()));

            //if (num > 0)
            //{
            //    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('存在相同的用户名，请修改')", true);
               
            //    return;
            //}

            info.Open("select * from userinfo where userid = '" + Request.QueryString["uid"].ToString() + "'");
            info.NextRow();

            info["username"] = txtUserName.Text;

            if (txtPwd.Text != string.Empty)
                info["password"] = Common.WebEncrypt(txtPwd.Text);

            info["realname"] = ddlRealname.SelectedItem.Text;
            info["StaffId"] = ddlRealname.SelectedValue;
          //  info["WarehouseId"] = ddlWarehous.SelectedValue;
            info["deptid"] = ddlDept.SelectedValue;
            info["roleid"] = ddlRole.SelectedValue;
            info["userbh"] = txtbh.Text;
            info["IP"] = txtIP.Text;//ip限制,多个ＩＰ以","号分开

            info.Update();
        }

        info.Close();
        Response.Redirect("User.aspx?moduid="+hidModuid.Value);
        //if (hidDeptid.Value == "")
        //    Response.Redirect("over.aspx");
        //else
        //    Response.Redirect("over.aspx?deptid=" + hidDeptid.Value);
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (hidDeptid.Value == "")
            Response.Redirect("User.aspx");
        else
            Response.Redirect("User.aspx?deptid=" + hidDeptid.Value);
    }
    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        ZSql zs = new ZSql();
        zs.Open("select staffid,staffname from CS_Staff where deptId="+ddlDept.SelectedValue);
        if (zs.m_table.Rows.Count > 0)
        {
            zs.Open("select staffid,staffname from CS_Staff where AuditState=1 and deptId=" + ddlDept.SelectedValue);
            if (zs.m_table.Rows.Count > 0)
 
            {
                ddlRealname.DataSource = zs.m_table;
                ddlRealname.DataTextField = "staffname";
                ddlRealname.DataValueField = "staffid";
                ddlRealname.DataBind();
                ddlRealname.Items.Insert(0, new ListItem("选择人员", "0"));
                zs.Close();
            }
            else
	        {
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('员工未审核！')", true);
                ddlRealname.Items.Insert(0, new ListItem("选择人员", "0"));
	        }
        }
      else
	    {
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('该部门员工为空请添加员工！')", true);
	    }
       
      
       
    }

    //private void initWare() 
    //{
    //    ZSql ware = new ZSql();
    //    ware.Open("select warehouseid,warehousename from CS_Warehouse");
    //    if (ware.m_table.Rows.Count > 0)
    //    {
    //        ware.Open("select warehouseid,warehousename from CS_Warehouse where AuditState='已通过'");
    //        if (ware.m_table.Rows.Count > 0)
    //        {
    //            ddlWarehous.DataSource = ware.m_table;
    //            ddlWarehous.DataTextField = "warehousename";
    //            ddlWarehous.DataValueField = "warehouseid";
    //            ddlWarehous.DataBind();
    //            ddlWarehous.Items.Insert(0, new ListItem("选择仓库", "0"));

    //            ware.Close();
    //        }
    //        else
    //        {
    //           ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('仓库未审核！')", true);
    //           ddlWarehous.Items.Insert(0, new ListItem("选择仓库", "0"));
    //        }
    //    }
    //    else
    //    {
    //        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('仓库为空请添加仓库！')", true);
    //        ddlWarehous.Items.Insert(0, new ListItem("选择仓库", "0"));
    //    }



    //}
}
